
/**   
*    
* 项目名称：a_yongan   
* 类名称：GoodTypeServiceImpl   
* 类描述：   
* 创建人：liubo   
* 创建时间：2018年6月18日 下午2:10:43    
* 修改时间：2018年6月18日 下午2:10:43   
* 修改备注：   
* @version    
*    
*/
package com.yongan.warehouse.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import com.yongan.user.enums.UserStatEnum;
import com.yongan.user.exception.UserException;
import com.yongan.util.Execution;
import com.yongan.warehouse.dao.GoodsinformationDao;
import com.yongan.warehouse.entity.Goodsinformation;
import com.yongan.warehouse.service.GoodsinformationService;

/**
 * @author liubo
 *
 */
@Service
public class GoodsinformationServiceImpl implements GoodsinformationService{

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private GoodsinformationDao goodsinformationDao;
	private final String slat = "dsjdajsdkHJJHAJAKdsd921e0p12*DAe#@$@!qew";
	@Override
	public List<Goodsinformation> getGoodsinformationList(int offset, int limit) {
		return goodsinformationDao.queryAll(offset, limit);
	}

	
	@Override
	public Goodsinformation getGoodsinformationById(String goodsinformationId) {
		
		return goodsinformationDao.queryById(goodsinformationId);
	}

	
	@Override
	public Execution executeGoodsinformation(Goodsinformation goodsinformation, String goodsinformationId,
			String md5) throws UserException {
		if(md5 == null || !md5.equals(getMD5(goodsinformationId))){
			//throw new UserException("user data rewrite");
		}
		try{
			if(goodsinformation == null){
				int deleteCount = goodsinformationDao.deleteGoodsinformation(goodsinformationId);
				if(deleteCount<=0){
					throw new UserException("delete failed");
				}else{
					return new Execution(goodsinformationId, UserStatEnum.SUCCESS);
				}
			}else if((goodsinformationId.equals(""))){
				int addCount = goodsinformationDao.insertGoodsinformation(goodsinformation);
				if(addCount<=0){
					throw new UserException("add failed");
				}else{
					return new Execution(goodsinformation.getGoodsinformationId(), UserStatEnum.SUCCESS);
				}
			}else if(!(goodsinformationId.equals(""))){
				int updateCount = goodsinformationDao.updateGoodsinformation(goodsinformation);
				if(updateCount<=0){
					throw new UserException("update failed");
				}else{
					return new Execution(goodsinformationId, UserStatEnum.SUCCESS);
				}
			}else{
				throw new UserException("parameter error");
			}
			
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			throw new UserException("inner error:"+e.getMessage());
		}
	}

	public int getCount() {
		
		return goodsinformationDao.queryCount();
	}
	private String getMD5(String Id){
		String base = Id + "/" + slat;
		String md5 = DigestUtils.md5DigestAsHex(base.getBytes());
		return md5;
	}


	@Override
	public List<Goodsinformation> getAllGoodsinformationList() {
		return goodsinformationDao.queryAllTree();
	}

}
